package com.tarena.lbs.activity.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.activity.dao.mapper.ActivityMapper;
import com.tarena.lbs.activity.dao.repository.ActivityRepository;
import com.tarena.lbs.activity.pojo.po.ActivityPO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
@RequiredArgsConstructor
public class ActivityRepositoryImpl implements ActivityRepository {

    private final ActivityMapper activityMapper;

//    @Override
//    public Page<ActivityPO> pageQuery(ActivityQuery query) throws BusinessException{
//        int pageNo = query.getPageNo() == null ? 1 : query.getPageNo();
//        int pageSize = query.getPageSize() == null ? 10 : query.getPageSize();
//        Page<ActivityPO> page = new Page<>(pageNo, pageSize);
//        LambdaQueryWrapper<ActivityPO> queryWrapper = new LambdaQueryWrapper<>();
//
//        // 条件拼接
//        queryWrapper.like(query.getActivityName() != null, ActivityPO::getActivityName, query.getActivityName())
//                .eq(query.getDeliveryChannel() != null, ActivityPO::getDeliveryChannel, query.getDeliveryChannel())
//                .eq(query.getType() != null, ActivityPO::getActivityType, query.getType())
//                .eq(query.getChannelType() != null, ActivityPO::getChannelType, query.getChannelType())
//                .eq(query.getStatus() != null, ActivityPO::getStatus, query.getStatus())
//                .eq(query.getBusinessId() != null, ActivityPO::getBusinessId, query.getBusinessId())
//                .ne(ActivityPO::getEnableStatus,ActivityStatusEnum.DELETED.getCode())
//                .orderByDesc(ActivityPO::getCreateAt);
//
//        // 处理时间范围
//        try {
//            if (query.getStartDate() != null) {
//                Date startDate = parseDate(query.getStartDate());
//                queryWrapper.ge(ActivityPO::getStartDate, startDate);
//            }
//            if (query.getEndDate() != null) {
//                Date endDate = parseEndDate(query.getEndDate());
//                queryWrapper.le(ActivityPO::getEndDate, endDate);
//            }
//        } catch (ParseException e) {
//            log.error("日期格式解析失败", e);
//            throw new BusinessException("-1", "日期格式错误，正确格式为 yyyy-MM-dd HH:mm:ss");
//        }
//
//        return activityMapper.selectPage(page, queryWrapper);
//    }


    @Override
    public void save(ActivityPO poParam) {
        activityMapper.insert(poParam);
    }

    @Override
    public List<ActivityPO> getActivities(Integer businessId) {
        QueryWrapper<ActivityPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id", businessId);
        return activityMapper.selectList(queryWrapper);
    }

    @Override
    public ActivityPO getActivityById(Integer activityId) {
        return activityMapper.selectById(activityId);
    }


    @Override
    public List<ActivityPO> getActivityByShopId(Integer shopId) {
        //select * from activity where FIND_IN_SET(#{shopId},shop_ids)
        //入参 3号店铺 有个活动绑定了 1,3,5 命中 绑定 3,6,9 命中
        return activityMapper.getActivityByShopId(shopId);
    }
}